<template>
  <div class="login">
    <div class="left">
      <div class="header">
        <img class="t1" src="@/assets/img/login_logo.png" alt />
        <span class="t2">黑马面面</span>
        <div class="t3"></div>
        <span class="t4">用户登录</span>
      </div>
      <el-form class="form" :model="form" ref="form" :rules="rules">
        <el-form-item prop="phone">
          <el-input prefix-icon="el-icon-user" placeholder="请输入手机号" v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            show-password
            v-model="form.password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-row>
            <el-col :span="16">
              <el-input placeholder="请输入验证码" prefix-icon="el-icon-key" v-model="form.code"></el-input>
            </el-col>
            <el-col :span="8">
              <img :src="baseUrl + codeUrl" alt class="code-img"  @click="getCode" />
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item prop="isPass">
          <el-checkbox v-model="form.isPass" :label="true">
            我已阅读并同意
            <el-link type="primary">用户协议</el-link>和
            <el-link type="primary">隐私条款</el-link>
          </el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn" @click="login">登录</el-button>
          <div></div>
          <el-button type="primary" class="btn m26" @click="$refs.register.isShow= true">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
    <img src="@/assets/img/login.png" alt />
    <register ref="register" />
  </div>
</template>

<script>
import {saveLocal,getLocal} from "@/utils/local.js"
import register from "./register";
import {toLogin} from "@/api/login.js"
export default {
  /* 测试分支合并 */
  components: {
    register,
  },
  created() {
   if(getLocal('token')){
     this.$router.push('/layout')
   }
  },
  data() {
    return {
      baseUrl: process.env.VUE_APP_URL,
      codeUrl: "/captcha?type=login",
      form: {
        phone: "",
        password: "",
        code: "",
        isPass: [],
      },
      rules: {
        phone: [
          { required: true, message: "请输入用户名", trigger: "change" },
         { validator:(rule,value,callback)=>{
            const _reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
           if( _reg.test(value)){
             callback()
           }else{
             callback(new Error('请输入正确的手机号'))
           }
          }}
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
          { min: 5, max: 12, message: "请输入5-12位的密码", trigger: "change" },
        ],
        code: [
          { required: true, message: "请输入验证码", trigger: "change" },
          { min: 4, max: 4, message: "请输入4位的验证码", trigger: "change" },
        ],
        isPass: [{ required: true, message: "必填", trigger: "change" }],
      },
    };
  },
  methods: {
    getCode(){
      this.codeUrl = '/captcha?type=login&'+ Math.random()
    },
    login() {
      this.$refs.form.validate((res) => {
        if (res) {
         toLogin(this.form).then((result)=>{
         /*   window.console.log(result) */
           saveLocal('token',result.data.data.token)
           this.$message.success('登录成功')
           this.$router.push('/layout')
         })
        } else {
          this.$message.error("验证失败");
        }
      });
    },
    showRegister() {
      this.$refs.register.isShow = true;
    },
  },
};
</script>

<style lang="less">
.login {
  display: flex;
  background: linear-gradient(#1493fa 28%, #01c6fa 96%);
  justify-content: space-around;
  align-items: center;
  height: 100%;
}
.left {
  width: 478px;
  height: 550px;
  background: #f5f5f5;
  padding: 44px 42px;
  .header {
    display: flex;
    align-items: center;
    .t1 {
      height: 25px;
    }
    .t2 {
      margin-left: 13px;
      margin-right: 13px;
      font-size: 24px;
      font-family: SourceHanSansCN Regular, SourceHanSansCN Regular-Regular;
      font-weight: 400;
      text-align: left;
      color: #0c0c0c;
    }
    .t3 {
      margin-right: 13px;
      width: 1px;
      height: 28px;
      background: #c7c7c7;
    }
    .t4 {
      font-size: 22px;
      font-family: PingFangSC Regular, PingFangSC Regular-Regular;
      font-weight: 400;
      text-align: left;
      color: #0c0c0c;
    }
  }
  .form {
    margin-top: 31px;
    .code-img {
      width: 100%;
      height: 40px;
    }
    .btn {
      width: 100%;
    }
    .m26 {
      margin-top: 26px;
    }
  }
}
</style>